<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="表单名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入表单名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开启状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择开启状态" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['activiti:actMaintainOrder:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['activiti:actMaintainOrder:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['activiti:actMaintainOrder:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['activiti:actMaintainOrder:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="actMaintainOrderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="表单名" align="center" prop="name" />
      <el-table-column label="开启状态" align="center" prop="status" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleDetail(scope.row)"
                     v-hasPermi="['activiti:actMaintainOrder:list']">详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['activiti:actMaintainOrder:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['activiti:actMaintainOrder:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!--表单配置详情-->
    <el-dialog title="表单详情" :visible.sync="detailOpen" width="50%" append-to-body>
      <div class="test-form">
        <parser :key="new Date().getTime()" :form-conf="detailForm" @submit="submitDetailForm" />
      </div>
    </el-dialog>

    <!-- 添加或修改维护单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="编号" prop="id">
          <el-input v-model="form.id" placeholder="请输入编号" />
        </el-form-item>
        <el-form-item label="编码" prop="desfromCode">
          <el-input v-model="form.desfromCode" placeholder="请输入编码" />
        </el-form-item>
        <el-form-item label="名称" prop="desfromName">
          <el-input v-model="form.desfromName" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="表单内容json串" prop="desfromJson">
          <el-input v-model="form.desfromJson" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listActMaintainOrder, getActMaintainOrder, delActMaintainOrder, addActMaintainOrder, updateActMaintainOrder ,decodeFields,test_mock,createProcessInstance} from "@/api/system/activiti/actMaintainOrder";
import Parser from '@/components/parser/Parser';

export default {
  name: "ActMaintainOrder",
  components: {
    Parser
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 维护单表格数据
      actMaintainOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单详情
      detailOpen: false,
      detailForm: {
        fields: []
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "表单名不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "开启状态不能为空", trigger: "blur" }
        ],
        conf: [
          { required: true, message: "表单的配置不能为空", trigger: "blur" }
        ],
        fields: [
          { required: true, message: "表单项的数组不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询维护单列表 */
    getList() {
      this.loading = true;
      listActMaintainOrder(this.queryParams).then(response => {
        this.actMaintainOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleUpdateFormJson(row){
      //this.$router.push({name: 'FormEditor', query: {id: row.id}})
      this.$router.push({path: '/flowableFormEdit', query: {formKey: row.formKey}})
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push({
        path:'/flowableFormEdit',
        query:{
          formId: row.id
        }
      });
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      getActMaintainOrder(row.id).then(response => {
        // 设置值
        const data = response.data;
        this.detailForm = {
          ...JSON.parse(data.conf),
          fields: decodeFields(data.fields),
          formId:row.id
        }
        // 弹窗打开
        this.detailOpen = true
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        status: 0,
        conf: null,
        fields: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
        tenantId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.$router.push({
      //   path:"/bpm/manager/form/edit"
      // });
      this.$router.push({path: '/flowableFormEdit'})
    },
    /** 修改按钮操作 */
    // handleUpdate(row) {
    //   this.reset();
    //   const id = row.id || this.ids
    //   getActMaintainOrder(id).then(response => {
    //     this.form = response.data;
    //     this.open = true;
    //     this.title = "修改单定义";
    //   });
    // },
    submitDetailForm(params){
      if (!params) {
        return;
      }
      // 设置表单禁用
      const conf = params.conf;
      conf.disabled = true; // 表单禁用
      conf.formBtns = false; // 按钮隐藏
      // 提交表单，创建流程
      const variables = params.values;
      console.log("form: " + variables);
      createProcessInstance({
        formId: params.formId,
        formVariables: variables
      }).then(response => {
        this.msgSuccess("发起流程成功");
        // 关闭当前窗口
        // this.$tab.closeOpenPage();
        // this.$router.go(-1);
        this.detailOpen = false;
      }).catch(() => {
        conf.disabled = false; // 表单开启
        conf.formBtns = true; // 按钮展示
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateActMaintainOrder(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addActMaintainOrder(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除维护单编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delActMaintainOrder(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('activiti/actMaintainOrder/export', {
        ...this.queryParams
      }, `activiti_actMaintainOrder.xlsx`)
    }
  }
};
</script>
